using imitateMacQQ.WebApi.Configures;
using imitateMacQQ.WebApi.Domain.RealTime.Interface;
using imitateMacQQ.WebApi.Domain.RealTime.utils;
using imitateMacQQ.WebApi.Domain.Request.utils;
using imitateMacQQ.WebApi.Repository.EFCore.Model;

namespace imitateMacQQ.WebApi.Domain.RealTime.Service;

public class Messager : IMessager
{
    private readonly INLogHelper _logHelper;

    public Messager(INLogHelper logHelper)
    {
        _logHelper = logHelper;
    }

    public async Task<Request<bool>> RealTimeMessager(string account, string inc)
    {
        try
        {
            if (inc.Contains("circle"))
            {
                await using var dataDbContext = new DataDbContext();
                var info = dataDbContext.RemarksMes.Where(e => e.AcceptAccount == account && e.IsRead == false);
                if (info.Count() is 0)
                    return Request<bool>.Ok(false);
            }

            if (inc.Contains("request"))
            {
                await using var dataDbContext = new DataDbContext();
                var info = dataDbContext.Buddies.Where(e => e.ResultAccount == account && e.IsUsing == 3);
                if (info.Count() is 0)
                    return Request<bool>.Ok(false);
            }

            if (Utils.Continuer(inc) is 0)
                throw new Exception("Inc Is Error");
            return Request<bool>.Ok(true);
        }
        catch (Exception ex)
        {
            _logHelper.LogError(ex);
            return Request<bool>.Ok(false, false, ex.Message);
        }
    }
}